#include "thumbnailsplugin.h"

#include <KPluginFactory>
#include <KPluginLoader>
#include <KAction>
#include <KActionCollection>
#include <KDialog>
#include "thumbnailswidget.h"

#include <kdclib.h>

class ThumbnailsPlugin;

#include <KLocale>
#include <interfaces/kdcjobsinterface.h>
KDCJobsInterface *gKDCJobsInterface;
#include <interfaces/kdcframegeneratorinterface.h>
#include <interfaces/kdcapplicationinterface.h>
KDCFrameGeneratorInterface *gKDCFrameGenerator;

K_PLUGIN_FACTORY( KDCMenuPluginFactory, registerPlugin< ThumbnailsPlugin >(); )
K_EXPORT_PLUGIN( KDCMenuPluginFactory( "ThumbnailsPlugin" ) )

ThumbnailsPlugin::ThumbnailsPlugin( QObject *parent, const QVariantList &args ) : KDCPluginInterface( parent ), pInfo( KPluginInfo("thumbnailsplugin.desktop", "services") ), pObject( NULL ), pInterfaces( NULL ), pKDCGuiInterface( NULL )
{
	Q_UNUSED( args );
	gKDCJobsInterface = NULL;
	gKDCFrameGenerator = NULL;
}

ThumbnailsPlugin::~ThumbnailsPlugin()
{
	unload();
}

QString ThumbnailsPlugin::interfaceName() const
{
	return "ThumbnailsPlugin";
}

const KPluginInfo& ThumbnailsPlugin::info() const
{
	return pInfo;
}

QWidget* ThumbnailsPlugin::widget() const
{
	return NULL;
}

KDCConfigPageInterface* ThumbnailsPlugin::configPage() const
{
	return NULL;
}

KDC::Position ThumbnailsPlugin::tabPosition() const
{
	return KDC::None;
}

bool ThumbnailsPlugin::load()
{
	KAction *thumbAction = new KAction( KIcon( pInfo.icon() ), i18n("Create thumbnails"), this );
	actionCollection()->addAction( "thumbnails", thumbAction );
	connect( thumbAction, SIGNAL( triggered() ), this, SLOT( createThumbnails() ) );
	
	setXMLFile("thumbnailsplugin.rc");
	return true;
}

bool ThumbnailsPlugin::unload()
{
	if ( pKDCGuiInterface )
		pKDCGuiInterface->removeGuiPlugin( this );
	return true;
}

bool ThumbnailsPlugin::setInterfaces( QList<KDCBaseInterface*>& aInterfaces )
{
	pInterfaces = &aInterfaces;
	return true;
}

bool ThumbnailsPlugin::init()
{
	foreach ( KDCBaseInterface *interface, *pInterfaces )
		if ( interface->interfaceName() == "KDCJobsInterface" )
			gKDCJobsInterface = static_cast<KDCJobsInterface*>( interface );
		else
			if ( interface->interfaceName() == "KDCGuiInterface" )
				pKDCGuiInterface = static_cast<KDCGuiInterface*>( interface );
		else
			if ( interface->interfaceName() == "KDCDiscStructurePlugin" )
			{
				connect( static_cast<KDCPluginInterface*>( interface ), SIGNAL( objectChanged( KDCObject * ) ), this, SLOT( setObject( KDCObject * ) ) );
			}
		else
			if ( interface->interfaceName() == "KDCFrameGeneratorInterface" )
				gKDCFrameGenerator = static_cast<KDCFrameGeneratorInterface*>( interface );
	if ( pKDCGuiInterface )
		pKDCGuiInterface->addGuiPlugin( this );
	return true;
}

void ThumbnailsPlugin::setObject( KDCObject *aObject )
{
	pObject = aObject;
}

void ThumbnailsPlugin::refresh()
{
	
}

KDCObject* ThumbnailsPlugin::currentObject() const
{
	return pObject;
}

void ThumbnailsPlugin::createThumbnails()
{
	if ( !pObject || pObject->object() != KDC::Movie )
		return;
	KDCMovie *movie = static_cast<KDCMovie*>( pObject );
	KDialog dlg;
	dlg.setCaption( i18n("Create thumbnails from file") + QString( " %1" ).arg( movie->metaData()->name() ) );
	dlg.setButtons( KDialog::Ok | KDialog::Cancel );
	dlg.setMainWidget( new ThumbnailsWidget( movie ) );
	
	foreach ( KDCBaseInterface *interface, *pInterfaces )
		if ( interface->interfaceName() == "KDCApplicationInterface" )
			dlg.setWindowIcon( static_cast<KDCApplicationInterface*>( interface )->appIcon() );
	
	dlg.resize( 800, 600 );
	dlg.exec();
}

#include "thumbnailsplugin.moc"
